package persistency.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import model.Business;
import model.BusinessTypeEnum;
import model.CourseHistory;
import model.OfferedCourse;
import model.Registration;
import model.Teacher;
import persistency.ArgIO;

public class RegistrationController extends BusinessController{
	private StringBuilder callableStatement;

	private static RegistrationController instance = new RegistrationController();
	private RegistrationController(){}
	
	public static RegistrationController instance(){
		return instance;
	}
	
	public boolean insertRegistration(Registration registration) {
		callableStatement = new StringBuilder("{call sp_insertregistration(?,?)}");
		return insert(registration,callableStatement,2);
	}

	public Registration getRegistration(String id){
		return (Registration)getBusinessObject(id,new StringBuilder("{call sp_getbyidregistration(?)}"),BusinessTypeEnum.REGISTRATION);
	}
	
	public Collection<Registration> getAll() {
		Collection<Registration> col = new ArrayList<Registration>();
		col.addAll((Collection<? extends Registration>) getBusinessObjects(new StringBuilder("{call sp_getallregistration}"),BusinessTypeEnum.REGISTRATION));
		return col;
	}
	
	public void deleteRegistration(Object id){
		//sp_deleteregistration
	}
	
	public Collection<Registration> getByOfferedCourseIdRegistrations(int id) {
		Collection<Registration> col = new ArrayList<Registration>();
		col.addAll((Collection<? extends Registration>) getBusinessObjects(id,new StringBuilder("{call sp_getbyofferedcourseidregistrations(?)}"),BusinessTypeEnum.REGISTRATION,java.sql.Types.INTEGER));
		return col;
		
	}
	
	public Collection<Registration> getByStudentIdRegistrations(String id) {
		Collection<Registration> col = new ArrayList<Registration>();
		col.addAll((Collection<? extends Registration>) getBusinessObjects(id,new StringBuilder("{call sp_getbystudentidregistrations(?)}"),BusinessTypeEnum.REGISTRATION,java.sql.Types.CHAR));
		return col;
		
	}
	
	@Override
	public void prepare(Business business, Object[] args, int[] argsType, ArgIO[] argsIO, int i) throws Exception {
		/*
    in studentnumber varchar(25),
    in startdate date
		 */
		
		Registration registration = (Registration) business;
		switch (i) {
		case 0: {
			args[i] = registration.getStudent().getStudentNumber();
			argsType[i] = java.sql.Types.CHAR;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 1: {
			args[i] = registration.getRegistrationDate();
			argsType[i] = java.sql.Types.DATE;
			argsIO[i] = ArgIO.IN;
			break;
		}
		default:
			throw new Exception("Error while creating SP createCourse");
		}
		
	}

	public int getlastId() {
		return executeMaxQuery(new StringBuilder("{call sp_getlastregistrationid}"));
	}
}
